var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "maishu-chitu-react", "components/common/base-component", "./services/memberService", "maishu-ui-toolkit", "./error-handle", "common", "./services/weixinService", "services/service", "./services/localService", "json!websiteConfig"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const maishu_chitu_react_1 = require("maishu-chitu-react");
    const base_component_1 = require("components/common/base-component");
    const memberService_1 = require("./services/memberService");
    const ui = require("maishu-ui-toolkit");
    const error_handle_1 = require("./error-handle");
    const common_1 = require("common");
    const weixinService_1 = require("./services/weixinService");
    const service_1 = require("services/service");
    const localService_1 = require("./services/localService");
    // import { contextName } from "../constants";
    const websiteConfig = require("json!websiteConfig");
    let modulesPath = "mobile/modules";
    class UserApplication extends maishu_chitu_react_1.Application {
        constructor(container) {
            super({ container });
            this.pageTagName = "article";
            this._forward = super.forward;
            let localService = this.createService(localService_1.LocalService);
            this.pageCreated.add((sender, page) => __awaiter(this, void 0, void 0, function* () {
                let name = page.name == "page" ? "" : page.name.split("/").join("-");
                page.element.className = "mobile-page " + name;
                let files = yield localService.clientFiles();
                let lessFilePath = `${modulesPath}/${page.name}.less`;
                let pageLessFiles = files.filter(o => o.startsWith(modulesPath) && o.endsWith(".less"));
                if (pageLessFiles.indexOf(lessFilePath) >= 0)
                    this.loadLess(lessFilePath);
            }));
            let memberService = this.createService(memberService_1.MemberService);
            memberService.store().then(o => {
                let styleColor = "default";
                if (o != null && o.Style != null)
                    styleColor = o.Style;
                base_component_1.BaseComponent.loadLessFile("baseStyle", styleColor, "components/style/baseStyle");
            });
            this.error.add((sender, error) => error_handle_1.errorHandle(error, sender));
            this.createDialogContainer();
        }
        loadjs(path) {
            return new Promise((reslove, reject) => {
                requirejs({ context: websiteConfig.requirejs.context }, [path], function (result) {
                    reslove(result);
                }, function (err) {
                    reject(err);
                });
            });
        }
        getWX() {
            return __awaiter(this, void 0, void 0, function* () {
                if (this.wx)
                    return this.wx;
                return new Promise((resolve, reject) => {
                    requirejs(["https://res.wx.qq.com/open/js/jweixin-1.3.2.js"], (wx) => {
                        this.wx = wx;
                        resolve(wx);
                    }, err => {
                        reject(err);
                    });
                });
            });
        }
        forward(pageUrl, args) {
            let url = encodeURIComponent(pageUrl);
            if (!weixinService_1.isWeixin) {
                return this._forward.apply(this, [pageUrl, args]);
            }
            this.getWX().then(wx => {
                wx.miniProgram.getEnv((res) => {
                    if (res.miniprogram) {
                        let routeData = this.parseUrl(url);
                        args = Object.assign({}, args || {}, routeData.values);
                        let obj = encodeURIComponent(JSON.stringify(args));
                        wx.miniProgram.navigateTo({ url: `../webview-page/webview-page?url=${routeData.pageName}&args=${obj}` });
                        return;
                    }
                    this._forward.apply(this, [pageUrl, args]);
                });
            });
            return null;
        }
        // 处理通过 URL 传过来的数据, 一般用于小程序
        // 从小程序主程序传给浏览器
        processURLParams() {
            return __awaiter(this, void 0, void 0, function* () {
                let token = common_1.urlParams["token"];
                if (token != null) {
                    service_1.Service.token.value = token;
                }
            });
        }
        run() {
            const _super = Object.create(null, {
                run: { get: () => super.run }
            });
            return __awaiter(this, void 0, void 0, function* () {
                yield this.processURLParams();
                _super.run.call(this);
            });
        }
        createDialogContainer() {
            let dialogContainer = document.createElement("div");
            dialogContainer.className = "mobile-page";
            document.body.appendChild(dialogContainer);
            ui.dialogConfig.dialogContainer = dialogContainer;
        }
        loadLess(filePath) {
            requirejs({ context: websiteConfig.contextName }, [`less!${filePath}`], () => {
            });
        }
    }
    exports.UserApplication = UserApplication;
    exports.app = window[common_1.USER_APP] = window[common_1.USER_APP] || new UserApplication();
});
